
/*
 * @Author: 邹威鹏(944740645@qq.com)
 * @remarks: 过滤http请求,返回了一个promise方便使用async语法糖
 */
// const baseUrl = 'https://admin.kenput.cn';
const baseUrl = 'http://ycsky.natapp1.cc';
const appId = 'wxe4f65fd9f5a232f9'
// 接受一个obj
// 例: axios({method:'post','https://admin.kenput.cn',{id:'1001','price:'1000'}})
const axios = (params = {
    url,
    data: '',
    header: '',
    method: 'get'
}) => {
    console.log(params);
    params.url = baseUrl + params.url;
    // 过滤 {appid},把带有{appid}的转成上面的appid 'wxe4f65fd9f5a232f9'
    if (params.url.match(/({appid})/g))
        params.url = params.url.split(/({appid})/g).filter(item => item != '{appid}').join(appId);
    return new Promise((resolve, resject) => {
        wx.request({
            url: params.url,
            data: params.data || '',
            method: params.method || 'GET',
            timeout: 2000,
            header: {
                // 'content-type': 'application/json',
                ...params.header
            },
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                console.log('请求错误:',err);
                resject({
                    err: err,
                    msg: `error:${err}`,
                })
                // console.log("请求失败", method, url, data, err)
            },
            complete: () => { }
        });
    })
}
export default axios;